package biz

import (
	"New-lesson-part2/custom_error"
	"New-lesson-part2/internal"
	"New-lesson-part2/model"
	"New-lesson-part2/proto/pb"
	"context"
	"errors"
	"github.com/golang/protobuf/ptypes/empty"
)

type OrderServiceServer struct {
}

func (o OrderServiceServer) OrderList(ctx context.Context, req *pb.OrderPagingReq) (*pb.OrderItemListRes, error) {
	var orderList []model.OrderItem
	var res pb.OrderItemListRes
	var total int64
	tx := internal.DB.Where(&model.OrderItem{AccountId: req.AccountId}).Begin()
	defer tx.Commit()
	r := tx.Count(&total)
	if total < 1 {
		tx.Rollback()
		return nil, errors.New(custom_error.OrderIsNotFound)
	}
	if r.Error != nil {
		tx.Rollback()
		return nil, errors.New(custom_error.OrderSystemError)
	}
	res.Total = int32(total)
	r = tx.Scopes(internal.MyPaging(req.PageNo, req.PageSize)).Find(&orderList)
	for _, item := range orderList {
		res.ItemListRes = append(res.ItemListRes, ConvertOrderModel2Pb(item))
	}

	return &res, nil
}

func (o OrderServiceServer) OrderDetail(ctx context.Context, req *pb.OrderItemReq) (*pb.OrderItemDetailRes, error) {
	var orderDetail model.OrderItem
	var detailRes pb.OrderItemDetailRes
	r := internal.DB.Where(&model.OrderItem{
		BaseModel: model.BaseModel{ID: req.Id},
		AccountId: req.AccountId,
	}).First(&orderDetail)
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.OrderIsNotFound)
	}
	res := ConvertOrderModel2Pb(orderDetail)
	detailRes.Order = res
	var orderProductList []model.OrderProduct
	r = internal.DB.Where(&model.OrderProduct{
		OrderId: orderDetail.ID,
	}).Find(&orderProductList)
	for _, i := range orderProductList {
		detailRes.ProductList = append(detailRes.ProductList, ConvertOrderProductModel2Pb(i))
	}

	return &detailRes, nil
}

func (o OrderServiceServer) ChangeOrderStatus(ctx context.Context, req *pb.OrderStatus) (*empty.Empty, error) {
	r := internal.DB.Model(&model.OrderItem{OrderNo: req.OrderNo}).
		Where(&model.OrderItem{Status: req.Status}).
		Update("status=?", req.Status) //订单编号要在前面的web端做一下校验.!=0
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.OrderIsNotFound)
	}
	return &empty.Empty{}, nil
}

func (o OrderServiceServer) CreateOrder(ctx context.Context, req *pb.OrderItemReq) (*pb.OrderItemRes, error) {
	/*
		1.拿到购物车内的选中商品信息
		2.计算订单总金额 product_srv
		3.扣减库存 stock_srv
		4.把数据写到数据库中 orderItem和orderproduct
		5.删除购物车中的产品
	*/
	var productIds []int32
	var cartList []model.ShopCart
	productNumMap := make(map[int32]int32)
	r := internal.DB.Where(&model.ShopCart{AccountId: req.AccountId, Checked: true}).Find(&cartList) //查询购物车中有哪些商品是选中的.
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.ProductNotChecked)
	}
	for _, i := range cartList {
		productIds = append(productIds, i.ProductId)
		productNumMap[i.ProductId] = i.Num
	}

	internal.DB.Where("product")

	/////未完
	return nil, nil
}

func ConvertOrderModel2Pb(req model.OrderItem) *pb.OrderItemRes {
	r := &pb.OrderItemRes{
		Id:        req.ID,
		AccountId: req.AccountId,
		PayType:   req.PayType,
		OrderNo:   req.OrderNo,
		PostCode:  req.PostCode,
		Amount:    req.OrderAmount,
		Addr:      req.Addr,
		Receiver:  req.Receiver,
		Mobile:    req.ReceiverMobile,
		Status:    req.Status,
	}
	return r
}

func ConvertOrderProductModel2Pb(pro model.OrderProduct) *pb.OrderProductRes {
	r := &pb.OrderProductRes{
		Id:          pro.ID,
		OrderId:     pro.OrderId,
		ProductId:   pro.ProductId,
		Num:         pro.Num,
		ProductName: pro.ProductName,
		RealPrice:   pro.RealPrice,
		CoverImage:  pro.CoverImage,
	}
	return r
}
